package com.briup.corejava.day20;

import java.io.*;
import java.util.Properties;

/**
 * 使用Properties类读取properties文件中的内容
 */
public class Test4 {
    public static void main(String[] args) {
        // 准备文件
        File file = new File("src/com/briup/corejava/day20/file/db.properties");
        // 做成输入流
        FileInputStream fis = null;
        // 准备输出流更新文件
        FileOutputStream fos = null;
        // 准备类型
        Properties properties = new Properties();
        try {
            fis = new FileInputStream(file);
            fos = new FileOutputStream(file);
            // 加载
            properties.load(fis);
            // 获取数据（key和value都是字符串的验证）
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            // 输出
            System.out.println("driver = " + driver);
            System.out.println("url = " + url);
            System.out.println("username = " + username);
            System.out.println("password = " + password);
            // 想通过代码修改文件的内容怎么办？
            properties.setProperty("max","10");
            properties.setProperty("min","1");
            System.out.println(properties);
            properties.store(fos,"增加了最大值和最小值");
            System.out.println("文件更新成功！");


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
